import json
import urllib
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.edge.options import Options as EdgeOptions
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
import time
from datetime import datetime


class WebScraper:
    def __init__(self, city_code, query, page, browser='chrome'):
        self.city_code = city_code
        self.query = query
        self.page = page
        self.browser = browser.lower()

        if self.browser == 'chrome':
            self.chrome_options = ChromeOptions()
            self.chrome_options.add_argument("--window-size=1920x1080")  # 设置窗口大小
            self.service = ChromeService(ChromeDriverManager().install())
            self.driver = webdriver.Chrome(service=self.service, options=self.chrome_options)
        elif self.browser == 'edge':
            self.edge_options = EdgeOptions()
            self.edge_options.add_argument("--window-size=1920x1080")  # 设置窗口大小
            self.service = EdgeService(EdgeChromiumDriverManager().install())
            self.driver = webdriver.Edge(service=self.service, options=self.edge_options)
        else:
            raise ValueError("Unsupported browser. Please choose 'chrome' or 'edge'.")

    def fetch_job_data(self):
        retries = 0
        max_retries = 5  # 设置最大重试次数
        all_job_data = []  # 初始化一个空列表来存储所有页面的数据

        while retries < max_retries:
            # 生成时间戳
            timestamp = datetime.now().strftime('%Y%m%d%H%M%S')

            # 打开目标网页
            self.driver.get(
                f"https://www.zhipin.com/web/geek/job?query={self.query}&city={self.city_code}&page={self.page}")

            # 等待页面加载
            time.sleep(20)

            # 获取XHR请求的数据
            xhr_url = "https://www.zhipin.com/wapi/zpgeek/search/joblist.json"
            params = {
                "scene": "1",
                "query": self.query,
                "city": self.city_code,
                "experience": "",
                "payType": "",
                "partTime": "",
                "degree": "",
                "industry": "",
                "scale": "",
                "stage": "",
                "position": "",
                "jobType": "",
                "salary": "",
                "multiBusinessDistrict": "",
                "multiSubway": "",
                "page": str(self.page),
                "pageSize": "30"
            }

            # 使用Selenium发送请求
            response = self.driver.execute_script(f"""
                var xhr = new XMLHttpRequest();
                xhr.open('GET', '{xhr_url}?{urllib.parse.urlencode(params)}', false);
                xhr.setRequestHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
                xhr.setRequestHeader('Referer', 'https://www.zhipin.com/web/geek/job?query={self.query}&city={self.city_code}&page=1');
                xhr.send(null);
                return xhr.responseText;
            """)
            data = json.loads(response)
            print(data)

            # 检查 code 是否为 0
            if data.get('code') == 0:
                # 关闭浏览器
                self.driver.quit()

                # 提取 jobList 数据
                job_list = data['zpData']['jobList']
                all_job_data.extend(job_list)  # 将当前页的数据添加到总列表中

                return all_job_data  # 成功获取数据后返回数据
            else:
                print(f"Code is not 0, retrying... ({retries + 1}/{max_retries})")
                self.driver.quit()
                retries += 1
                time.sleep(5)  # 等待一段时间后重试

        print(f"达到最大重试次数 {max_retries}，程序退出。")
        self.driver.quit()
        return None  # 返回 None 表示失败
